package com.asset.salesforce.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.domain.ChatterContribution;
import com.asset.salesforce.domain.Comment;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.File;
import com.asset.salesforce.domain.Post;
import com.asset.salesforce.domain.UpdateChatter;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.ChatterContributionService;
import com.asset.salesforce.service.CityService;
import com.asset.salesforce.service.CommentService;
import com.asset.salesforce.service.CompanyService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.FileService;
//import com.asset.salesforce.service.IndusteryService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.LeadService;
//import com.asset.salesforce.service.LeadSourceService;
//import com.asset.salesforce.service.LeadStatusService;
import com.asset.salesforce.service.LoginService;
import com.asset.salesforce.service.PostService;
//import com.asset.salesforce.service.RatingService;
import com.asset.salesforce.service.UpdateChatterService;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

@Controller
public class PostController {
	@Autowired
	private PostService postService;
	
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private UpdateChatterService updateChatterService;
	
	@Autowired
	private JoinRequestService joinRequestService;
	
	@Autowired
	private ChatterContributionService chatterContributionService;
	@Autowired
	private CalendarService calendarService;

	@Autowired
	private FileService fileService;
	@SuppressWarnings("unused")
	@RequestMapping(value="/chatter" ,method=RequestMethod.GET)
	public String getChatter(HttpSession session,Model model){
		Employee employee = (Employee)session.getAttribute("userDetails");
		List<Post> posts = new ArrayList<Post>();
		posts = postService.getAllPostsByEmpId(employee);
		List<UpdateChatter> updateChatters = new ArrayList<UpdateChatter>();
		updateChatters	= updateChatterService.updateChatterWithFollow(employee.getId());
		//List<Post> posts2 = new ArrayList<Post>();
		//System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+updateChatters.size());
		//for (UpdateChatter updateChatter : updateChatters) {
			//String link = "<a href='";
			//if(updateChatter.getFollowType().getId() == 1){
			//	link += "viewAccount.html?id="+updateChatter.getFollowedId()+"'>"+updateChatter.getPost().getPostText()+"</a>";
			//	Post post = updateChatter.getPost();
			//	post.setPostText(link);
			//	posts2.add(post);
			//}else if(updateChatter.getFollowType().getId() == 3){
				//link += "viewLead.html?id="+updateChatter.getFollowedId()+"'>"+updateChatter.getPost().getPostText()+"</a>";
				//Post post = updateChatter.getPost();
				//post.setPostText(link);
				//posts2.add(post);
			//}else if(updateChatter.getFollowType().getId() == 2){
				
			//}
	//	}
		//posts.addAll(posts2);
		String currentChatter = "chatter";
		model.addAttribute("posts", posts);
		model.addAttribute("currentChatter", currentChatter);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "chatter";
	}
	@RequestMapping(value="/chatterme" ,method=RequestMethod.GET)
	public String getChatterByMe(HttpSession session,Model model){
		Employee employee = (Employee)session.getAttribute("userDetails");
		List<Post> posts = new ArrayList<Post>();
		posts = postService.getPostsByMe(employee);
		List<Post> arrangedPost = new ArrayList<Post>();
		int count = posts.size()-1;
		for (int i=count;i>=0;i--) {
			arrangedPost.add(posts.get(i));
		}
		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+arrangedPost.size());
		model.addAttribute("posts", arrangedPost);
		String currentChatter = "me";
		model.addAttribute("currentChatter", currentChatter);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "chatter";
	}
	@RequestMapping(value="/chattercompany" ,method=RequestMethod.GET)
	public String getChatterByCompany(HttpSession session,Model model){
		Employee employee = (Employee)session.getAttribute("userDetails");
		List<Post> posts = new ArrayList<Post>();
		List<Post> arrangedPost = new ArrayList<Post>();
		posts = postService.getPostsByCompany(employee);
		int count = posts.size()-1;
		for (int i=count;i>=0;i--) {
			arrangedPost.add(posts.get(i));
		}
		model.addAttribute("posts", arrangedPost);
		String currentChatter = "company";
		model.addAttribute("currentChatter", currentChatter);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "chatter";
	}
	
	@RequestMapping(value="/chatter" ,method=RequestMethod.POST)
	public  String postChatter(HttpSession session,Model model,HttpServletRequest request){
		 Employee employee = (Employee)session.getAttribute("userDetails");
		 ChatterContribution chatter = chatterContributionService.getChatterByEmployee(employee.getId());
		 chatter.setCountPosts(chatter.getCountPosts() + 1);
		 chatterContributionService.editChater(chatter);
		 Post post = new  Post();
		 post.setPoster(employee);
		 post.setPostText((String)request.getParameter("post"));
		 post.setPostDate(new Date());
		 postService.addPost(post);
		 
		 model.addAttribute("post", post);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
		 return "post";
	}
	
	@RequestMapping(value="/comment" ,method=RequestMethod.POST)
	public  String postComment(HttpSession session,Model model,HttpServletRequest request){
		Employee employee = (Employee)session.getAttribute("userDetails");
		Comment comment = new Comment();
		comment.setCommentDate(new Date());
		comment.setCommenter(employee);
		comment.setCommentText((String)request.getParameter("comment"));
		//System.out.println("((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))"+request.getParameter("comment").toString());
		ChatterContribution chatter = chatterContributionService.getChatterByEmployee(employee.getId());
		 chatter.setCountComments(chatter.getCountComments() + 1);
		 chatterContributionService.editChater(chatter);
		Post post = postService.getPostById(Integer.parseInt((String)request.getParameter("PostId")));
		comment.setPost(post);
		Employee poster = post.getPoster();
		ChatterContribution chatterPoster = chatterContributionService.getChatterByEmployee(employee.getId());
		chatterPoster.setCountRecivedComments(chatterPoster.getCountRecivedComments() +1);
		chatterContributionService.editChater(chatterPoster);
		commentService.addcomment(comment);
		
		model.addAttribute("comment", comment);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "comment";
	}
	public static String encodeImage(byte[] imageByteArray) {
        return Base64.encode(imageByteArray);
    }
}
